﻿Public Class PalilatinKeyboard
    Inherits System.Windows.Forms.TextBox
    Private EscapeFlag As Boolean   ' Set when user presses the back slash \
    Private mTypingStyle As String
    Private LastCh As String

    Protected Overrides Function ProcessKeyMessage(ByRef m As System.Windows.Forms.Message) As Boolean
        ' Get out if this is not a KeyUp message
        If m.Msg <> 258 Then Return False
        Const Delay As Integer = 100
        ' Obtain the Keystroke character
        Dim KeyChar As Integer = m.WParam.ToInt32
        ' Process a keystroke
        Dim chList(29) As String
        Dim strKetQua As String
        Dim dv As New DataView()

        chList(0) = "ā" 'aa
        chList(1) = "Ā" 'AA
        chList(2) = "ī" ' ii
        chList(3) = "Ī"
        chList(4) = "ū" ' uu
        chList(5) = "Ū"
        chList(6) = "ṅ" ' N
        chList(7) = "Ṅ"
        chList(8) = "ṃ"
        chList(9) = "Ṃ"
        chList(10) = "ñ"
        chList(11) = "Ñ"
        chList(12) = "ṭ" 'T
        chList(13) = "Ṭ"
        chList(14) = "ḍ"
        chList(15) = "Ḍ"
        chList(16) = "ṇ"
        chList(17) = "Ṇ"
        chList(18) = "ḷ"
        chList(19) = "Ḷ"
        chList(20) = "ṛ" ' 	 LATIN SMALL LET
        chList(21) = "Ṛ"
        chList(22) = "ṣ"
        chList(23) = "Ṣ"
        chList(24) = "ṝ" ' Ṝ
        chList(25) = "Ṝ" ' ś
        chList(26) = "ś"
        chList(27) = "Ś" ' ḹ
        chList(28) = "ḹ" ' Ḹ
        chList(29) = "Ḹ" ' ŏ ŏ
        'MsgBox("have" & KeyChar)

        If Me.TextLength = 0 Then Exit Function
        'If mTypingStyle = "Pali" Then
        Select Case KeyChar
            Case 97
                If Mid(Me.Text, Me.SelectionStart, 1) = "a" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(0)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 65
                If Mid(Me.Text, Me.SelectionStart, 1) = "A" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(1)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 105
                If Mid(Me.Text, Me.SelectionStart, 1) = "i" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(2)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 73
                If Mid(Me.Text, Me.SelectionStart, 1) = "I" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(3)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 117
                If Mid(Me.Text, Me.SelectionStart, 1) = "u" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(4)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 85
                If Mid(Me.Text, Me.SelectionStart, 1) = "U" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(5)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 110
                If Mid(Me.Text, Me.SelectionStart, 1) = ChrW(34) Then ' chList(6) = "ṅ"
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(6)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "~" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(10)  'chList(10) = "ñ"
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(16) 'chList(16) = "ṇ"
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 78
                If Mid(Me.Text, Me.SelectionStart, 1) = ChrW(34) Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(7) 'chList(7) = "Ṅ"
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "~" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(11)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(17)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 109 ' m
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(8)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 77 ' M
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(9)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 116 ' t
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(12)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 84 ' T
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(13)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 100 ' d
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(14)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 68 ' D
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(15)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 108 ' l
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(18)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "ḷ" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(28)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 76 ' L "Ḷ"
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(19)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "Ḷ" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(29)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 114 ' r
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(20)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "ṛ" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(24)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 82 ' R
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(21)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "Ṛ" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(25)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 115 ' s
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(22)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "`" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(26)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
            Case 83 ' S
                If Mid(Me.Text, Me.SelectionStart, 1) = "." Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(23)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "`" Then
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(27)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
        End Select
        'End If
        '-------------------------------
        ' KHU VUC TIM KIEM
        ' ------------------------------

        Try
            dv.Table = frmMain.ds.Tables(frmMain.DictTable)
            If frmMain.isWordSelect = True Then
                dv.RowFilter = "Words like '%" & Me.Text & "%'"
            Else
                dv.RowFilter = "Transcription like '%" & Me.Text & "%'"
            End If
            frmMain.listWords.DataSource = dv

            If KeyChar = Keys.Enter Then
                If frmMain.isWordSelect = True Then
                    frmMain.SearchWords(frmMain.DictTable, "Words", Me.Text)
                Else
                    frmMain.SearchWords(frmMain.DictTable, "Transcription", Me.Text)
                End If
            End If
        Catch
            Me.Text = Me.Text
        Finally

        End Try

        EscapeFlag = False  ' Reset the Escape Flag
    End Function
   
End Class
